package com.zhuzi.guicollect.controller.fx;

import com.zhuzi.guicollect.ui.MainView;
import com.zhuzi.guicollect.ui.WxView;
import com.zhuzi.guicollect.ui.XueQiuView;
import de.felixroske.jfxsupport.AbstractFxmlView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;

import javax.annotation.PostConstruct;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.ResourceBundle;

/**
 * @Author: MI
 * @Date: 2022/1/16/15:48
 * @Description:
 */
@Slf4j
@FXMLController
public class TabController implements Initializable {

    @FXML
    public TabPane tabPane;

    @Autowired
    ConfigurableApplicationContext applicationContext;

    // 视图map
    LinkedHashMap<String, AbstractFxmlView> viewMap;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        log.info("初始化页面~tabPane");
        ObservableList<Tab> tabs = tabPane.getTabs();
        viewMap.forEach((k, v) -> {
            System.out.println(k);
            Tab jsl = new Tab(k, v.getView());
            tabs.add(jsl);
        });
    }

    @PostConstruct
    public void mainViewinit() {
        //TODO 初始化 tab
        viewMap = new LinkedHashMap() {
            {
                put("jsl", applicationContext.getBean(MainView.class));
                put("微信公众号", applicationContext.getBean(WxView.class));
                put("雪球", applicationContext.getBean(XueQiuView.class));
            }
        };
    }
}
